<!--
 * @Descripttion: (打包任务/cks_packaging_task)
 * @Author: (admin)
 * @Date: (2025-08-25)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>

      <el-form-item label="任务号" prop="taskNo">
        <el-input v-model="queryParams.taskNo" placeholder="" clearable style="width: 240px" />
      </el-form-item>
      <el-form-item label="时间">
        <!--  :default-time="defaultTime2" -->
        <el-date-picker v-model="queryParams.dateRange" style="width: 360px" type="datetimerange"
          :default-time="defaultTime2" value-format="YYYY-MM-DD HH:mm:ss" range-separator="-" start-placeholder=""
          end-placeholder=""></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <!-- <el-button type="primary" v-hasPermi="['ckspackagingtask:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button> -->
      </el-col>
      <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
    </el-row>

    <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
      highlight-current-row @sort-change="sortChange">

      <el-table-column prop="taskNo" label="任务编号" align="center" width="160" :show-overflow-tooltip="true" />
      <el-table-column prop="containerNo" label="容器编号" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="waybillNo" label="面单号" align="center" width="160" :show-overflow-tooltip="true" />
      <!-- <el-table-column prop="orderNo" label="订单号" align="center" :show-overflow-tooltip="true" /> -->
      <!-- <el-table-column prop="packageNo" label="包裹号" align="center" :show-overflow-tooltip="true" /> -->
      <el-table-column prop="position" label="位置" :show-overflow-tooltip="true" />
      <el-table-column prop="taskStatusLabel" label="任务状态" :show-overflow-tooltip="true" />
      <el-table-column prop="createdTime" label="接收时间" width="160" :show-overflow-tooltip="true" />

      <el-table-column label="操作" width="120">
        <template #default="scope">
          <!-- <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['ckspackagingtask:edit']" @click="handleUpdate(scope.row)"></el-button> -->
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['ckspackagingtask:delete']"
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />


    <!-- 添加或修改打包任务对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">

          <el-col :lg="12" v-if="opertype != 1">
            <el-form-item label="Id" prop="id">
              <el-input-number v-model.number="form.id" controls-position="right" placeholder="请输入Id"
                :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="任务编号" prop="taskNo">
              <el-input v-model="form.taskNo" placeholder="请输入任务编号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="流转容器编号" prop="containerNo">
              <el-input v-model="form.containerNo" placeholder="请输入流转容器编号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="面单号" prop="waybillNo">
              <el-input v-model="form.waybillNo" placeholder="请输入面单号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="订单号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入订单号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="包裹号" prop="packageNo">
              <el-input v-model="form.packageNo" placeholder="请输入包裹号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="禁用(无效)状态" prop="nullity">
              <el-input v-model="form.nullity" placeholder="请输入禁用(无效)状态" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建时间" prop="createdTime">
              <el-date-picker v-model="form.createdTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="是否删除" prop="isDeleted">
              <el-radio-group v-model="form.isDeleted">
                <el-radio v-for="item in options.isDeletedOptions" :key="item.dictValue" :value="item.dictValue">
                  {{ item.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ckspackagingtask">
import {
  listCksPackagingTask,
  addCksPackagingTask, delCksPackagingTask,
  updateCksPackagingTask, getCksPackagingTask,
}
  from '@/api/business/ckspackagingtask.js'
  import dayjs from 'dayjs'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const defaultTime2 = [
    new Date(2000, 1, 1, 0, 0, 0),
    new Date(2000, 2, 1, 23, 59, 59),
]
const today = dayjs()
const startDay  = dayjs().subtract(3, 'day')

const defaultDateRange = [startDay.startOf('day').format('YYYY-MM-DD HH:mm:ss'),
today.endOf('day').format('YYYY-MM-DD HH:mm:ss')]
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
   dateRange:defaultDateRange,
    taskNo:''
})
const columns = ref([
  { visible: true, prop: 'id', label: 'Id' },
  { visible: true, prop: 'taskNo', label: '任务编号' },
  { visible: true, prop: 'containerNo', label: '流转容器编号' },
  { visible: true, prop: 'waybillNo', label: '面单号' },
  { visible: true, prop: 'orderNo', label: '订单号' },
  { visible: true, prop: 'packageNo', label: '包裹号' },
  { visible: true, prop: 'nullity', label: '禁用(无效)状态' },
  { visible: true, prop: 'createdTime', label: '创建时间' },
  { visible: false, prop: 'isDeleted', label: '是否删除' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]

function getQueryBody()
{
    const { dateRange ,pageNum,pageSize,taskNo } = queryParams
    const body = { pageNum,pageSize,taskNo }
    proxy.addDateRange(body, dateRange) 
    return body
}
function getList() {
  loading.value = true


  const body = getQueryBody()

  listCksPackagingTask(body).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    taskNo: [{ required: true, message: "任务编号不能为空", trigger: "blur" }],
    containerNo: [{ required: true, message: "流转容器编号不能为空", trigger: "blur" }],
    waybillNo: [{ required: true, message: "面单号不能为空", trigger: "blur" }],
    orderNo: [{ required: true, message: "订单号不能为空", trigger: "blur" }],
    packageNo: [{ required: true, message: "包裹号不能为空", trigger: "blur" }],
    nullity: [{ required: true, message: "禁用(无效)状态不能为空", trigger: "blur" }],
    createdTime: [{ required: true, message: "创建时间不能为空", trigger: "blur" }],
    isDeleted: [{ required: true, message: "是否删除不能为空", trigger: "blur" }],
  },
  options: {
    // 是否删除 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    isDeletedOptions: [],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    taskNo: null,
    containerNo: null,
    waybillNo: null,
    orderNo: null,
    packageNo: null,
    nullity: null,
    createdTime: null,
    isDeleted: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加打包任务'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getCksPackagingTask(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改打包任务'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateCksPackagingTask(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCksPackagingTask(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value
  const message = row.taskNo || ''
  proxy
    .$confirm('是否确认删除编号为"' + message + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delCksPackagingTask(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}





handleQuery()
</script>